﻿Imports System

Imports System.Reflection
Imports System.Xml
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.OneNote
Imports System.Text

Public Class TechEdOneNoteCreator

    Private strUnfiledNotesPath As String
    Private strNewPageID As String

    Private FormattedOneNotePage = _
  <?xml version="1.0"?>
  <one:Page xmlns:one="http://schemas.microsoft.com/office/onenote/2007/onenote" ID="{PageID}" name="{Title}" dateTime="{DateAndTime}" isCurrentlyViewed="true" style="font-weight:bold;font-family:Calibri;font-size:11.0pt" lang="en-US">
      <one:PageSettings RTL="false" color="{PageColor}">
          <one:PageSize>
              <one:Orientation landscape="false"/>
              <one:Dimensions width="792.0" height="1224.0"/>
              <one:Margins top="36.0" bottom="36.0" left="36.0" right="36.0"/>
          </one:PageSize>
          <one:RuleLines visible="true">
              <one:Horizontal color="#CAEBFD" spacing="13.42771530151367"/>
              <one:Margin color="#FF5050"/>
          </one:RuleLines>
      </one:PageSettings>
      <one:Title style="font-family:Calibri;font-size:17.0pt{TitleFontColor}" lang="en-US">
          <one:OE alignment="left">
              <one:T><![CDATA[{Title}]]></one:T>
          </one:OE>
      </one:Title>
      <one:Outline>
          <one:Position x="549.0" y="36.0" z="1"/>
          <one:Size width="207.1090393066406" height="58.76315689086914"/>
          <one:OEChildren>
              <one:OE alignment="left">
                  <one:Table bordersVisible="true">
                      <one:Columns>
                          <one:Column index="0" width="43.11008071899414"/>
                          <one:Column index="1" width="148.3689117431641" isLocked="true"/>
                      </one:Columns>
                      <one:Row>
                          <one:Cell>
                              <one:OEChildren>
                                  <one:OE alignment="left">
                                      <one:T><![CDATA[Track]]></one:T>
                                  </one:OE>
                              </one:OEChildren>
                          </one:Cell>
                          <one:Cell style="font-family:Calibri;font-size:11.0pt;background: white">
                              <one:OEChildren>
                                  <one:OE alignment="left">
                                      <one:T><![CDATA[{Track}]]></one:T>
                                  </one:OE>
                              </one:OEChildren>
                          </one:Cell>
                      </one:Row>
                      <one:Row>
                          <one:Cell>
                              <one:OEChildren>
                                  <one:OE alignment="left">
                                      <one:T><![CDATA[Format]]></one:T>
                                  </one:OE>
                              </one:OEChildren>
                          </one:Cell>
                          <one:Cell style="font-family:Calibri;font-size:11.0pt">
                              <one:OEChildren>
                                  <one:OE alignment="left">
                                      <one:T><![CDATA[{SessionFormat}]]></one:T>
                                  </one:OE>
                              </one:OEChildren>
                          </one:Cell>
                      </one:Row>
                      <one:Row>
                          <one:Cell>
                              <one:OEChildren>
                                  <one:OE alignment="left">
                                      <one:T><![CDATA[Location]]></one:T>
                                  </one:OE>
                              </one:OEChildren>
                          </one:Cell>
                          <one:Cell style="font-family:Calibri;font-size:11.0pt">
                              <one:OEChildren>
                                  <one:OE alignment="left">
                                      <one:T><![CDATA[{RoomNumber}]]></one:T>
                                  </one:OE>
                              </one:OEChildren>
                          </one:Cell>
                      </one:Row>
                  </one:Table>
              </one:OE>
          </one:OEChildren>
      </one:Outline>
      <one:Outline>
          <one:Position x="36.0" y="155.0863037109375" z="0"/>
          <one:Size width="686.7239990234374" height="93.99401092529296"/>
          <one:OEChildren>
              <one:OE alignment="left">
                  <one:T><![CDATA[Session Description]]></one:T>
              </one:OE>
              <one:OE alignment="left" style="font-family:Calibri;font-size:11.0pt">
                  <one:T><![CDATA[]]></one:T>
              </one:OE>
              <one:OE alignment="left" style="font-family:Calibri;font-size:11.0pt">
                  <one:T><![CDATA[{Body}]]></one:T>
              </one:OE>
          </one:OEChildren>
      </one:Outline>
      <one:Outline>
          <one:Position x="36.0" y="306.0" z="2"/>
          <one:Size width="157.9260559082031" height="140.8417358398437"/>
          <one:OEChildren>
              <one:OE alignment="left">
                  <one:T><![CDATA[Presenter Information]]></one:T>
              </one:OE>
              <one:OE alignment="left" style="font-family:Calibri;font-size:11.0pt">
                  <one:T><![CDATA[]]></one:T>
              </one:OE>
              <one:OE alignment="left">
                  <one:Table bordersVisible="true">
                      <one:Columns>
                          <one:Column index="0" width="88.11006927490234"/>
                          <one:Column index="1" width="589.1099853515624"/>
                      </one:Columns>
                      <one:Row>
                          <one:Cell>
                              <one:OEChildren>
                                  <one:OE alignment="left">
                                      <one:T><![CDATA[Name]]></one:T>
                                  </one:OE>
                              </one:OEChildren>
                          </one:Cell>
                          <one:Cell style="font-family:Calibri;font-size:11.0pt">
                              <one:OEChildren>
                                  <one:OE alignment="left">
                                      <one:T><![CDATA[]]></one:T>
                                  </one:OE>
                              </one:OEChildren>
                          </one:Cell>
                      </one:Row>
                      <one:Row>
                          <one:Cell>
                              <one:OEChildren>
                                  <one:OE alignment="left">
                                      <one:T><![CDATA[Company]]></one:T>
                                  </one:OE>
                              </one:OEChildren>
                          </one:Cell>
                          <one:Cell style="font-family:Calibri;font-size:11.0pt">
                              <one:OEChildren>
                                  <one:OE alignment="left">
                                      <one:T><![CDATA[]]></one:T>
                                  </one:OE>
                              </one:OEChildren>
                          </one:Cell>
                      </one:Row>
                      <one:Row>
                          <one:Cell>
                              <one:OEChildren>
                                  <one:OE alignment="left">
                                      <one:T><![CDATA[Title]]></one:T>
                                  </one:OE>
                              </one:OEChildren>
                          </one:Cell>
                          <one:Cell style="font-family:Calibri;font-size:11.0pt">
                              <one:OEChildren>
                                  <one:OE alignment="left">
                                      <one:T><![CDATA[]]></one:T>
                                  </one:OE>
                              </one:OEChildren>
                          </one:Cell>
                      </one:Row>
                      <one:Row>
                          <one:Cell>
                              <one:OEChildren>
                                  <one:OE alignment="left">
                                      <one:T><![CDATA[E-Mail Address]]></one:T>
                                  </one:OE>
                              </one:OEChildren>
                          </one:Cell>
                          <one:Cell style="font-family:Calibri;font-size:11.0pt">
                              <one:OEChildren>
                                  <one:OE alignment="left">
                                      <one:T><![CDATA[]]></one:T>
                                  </one:OE>
                              </one:OEChildren>
                          </one:Cell>
                      </one:Row>
                      <one:Row>
                          <one:Cell>
                              <one:OEChildren>
                                  <one:OE alignment="left">
                                      <one:T><![CDATA[Blog]]></one:T>
                                  </one:OE>
                              </one:OEChildren>
                          </one:Cell>
                          <one:Cell style="font-family:Calibri;font-size:11.0pt">
                              <one:OEChildren>
                                  <one:OE alignment="left">
                                      <one:T><![CDATA[]]></one:T>
                                  </one:OE>
                              </one:OEChildren>
                          </one:Cell>
                      </one:Row>
                      <one:Row>
                          <one:Cell>
                              <one:OEChildren>
                                  <one:OE alignment="left">
                                      <one:T><![CDATA[Other Information]]></one:T>
                                  </one:OE>
                              </one:OEChildren>
                          </one:Cell>
                          <one:Cell style="font-family:Calibri;font-size:11.0pt">
                              <one:OEChildren>
                                  <one:OE alignment="left">
                                      <one:T><![CDATA[]]></one:T>
                                  </one:OE>
                              </one:OEChildren>
                          </one:Cell>
                      </one:Row>
                  </one:Table>
              </one:OE>
          </one:OEChildren>
      </one:Outline>
      <one:Outline>
          <one:Position x="36.0" y="486.0" z="3"/>
          <one:Size width="93.51605224609375" height="40.28315734863281"/>
          <one:OEChildren>
              <one:OE alignment="left">
                  <one:T><![CDATA[Nuggets]]></one:T>
              </one:OE>
              <one:OE alignment="left" style="font-family:Calibri;font-size:11.0pt">
                  <one:T><![CDATA[]]></one:T>
              </one:OE>
              <one:OE alignment="left" style="font-family:Calibri;font-size:11.0pt">
                  <one:T><![CDATA[]]></one:T>
              </one:OE>
          </one:OEChildren>
      </one:Outline>
      <one:Outline>
          <one:Position x="36.0" y="558.0" z="4"/>
          <one:Size width="83.09605407714843" height="40.28315734863281"/>
          <one:OEChildren>
              <one:OE alignment="left">
                  <one:T><![CDATA[Notes]]></one:T>
              </one:OE>
              <one:OE alignment="left" style="font-family:Calibri;font-size:11.0pt">
                  <one:T><![CDATA[]]></one:T>
              </one:OE>
              <one:OE alignment="left" style="font-family:Calibri;font-size:11.0pt">
                  <one:T><![CDATA[]]></one:T>
              </one:OE>
          </one:OEChildren>
      </one:Outline>
      <one:Outline>
          <one:Position x="36.0" y="1026.0" z="6"/>
          <one:Size width="173.9855041503906" height="26.85546112060547"/>
          <one:OEChildren>
              <one:OE alignment="left">
                  <one:T><![CDATA[Recommended Resources]]></one:T>
              </one:OE>
              <one:OE alignment="left" style="font-family:Calibri;font-size:11.0pt">
                  <one:T><![CDATA[]]></one:T>
              </one:OE>
          </one:OEChildren>
      </one:Outline>
      <one:Outline>
          <one:Position x="36.0" y="1098.0" z="5"/>
          <one:Size width="80.23873138427734" height="40.28315734863281"/>
          <one:OEChildren>
              <one:OE alignment="left">
                  <one:T><![CDATA[Swag]]></one:T>
              </one:OE>
              <one:OE alignment="left" style="font-family:Calibri;font-size:11.0pt">
                  <one:T><![CDATA[]]></one:T>
              </one:OE>
              <one:OE alignment="left" style="font-family:Calibri;font-size:11.0pt">
                  <one:T><![CDATA[]]></one:T>
              </one:OE>
          </one:OEChildren>
      </one:Outline>
  </one:Page>




    Private Sub btnCreatePages_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreatePages.Click
        Dim oOutlookApplication As Microsoft.Office.Interop.Outlook.Application = New Microsoft.Office.Interop.Outlook.Application()
        Dim oNamespace As Microsoft.Office.Interop.Outlook.NameSpace = oOutlookApplication.GetNamespace("MAPI")

        oNamespace.Logon()

        Dim oSession As Microsoft.Office.Interop.Outlook.NameSpace = oOutlookApplication.Session
        Dim oCalendar As Microsoft.Office.Interop.Outlook.Folder = oSession.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar)

        ''        Dim oCalendar As Microsoft.Office.Interop.Outlook.Folder = oNamespace.OpenSharedFolder("webcal://northamerica.msteched.com/feeds/ics.ashx/yllwbb7xxwtbjf4xk5qirwteevnwf8t82g2x4zfstq2ou35uedqmmp5fspaxvnlp?programid=7cb4e861-1663-41d0-aa7d-60b27b1febe6")

        Dim oCalendarItems As Microsoft.Office.Interop.Outlook.Items = oCalendar.Items


        btnCreatePages.Enabled = False

        oCalendarItems.IncludeRecurrences = False
        oCalendarItems.Sort("[Start]")
        Dim strRestriction = "[Start] >= '06/06/2010' AND [End] < '06/11/2010'"
        Dim oRestrictedItems = oCalendarItems.Restrict(strRestriction)
        oRestrictedItems.IncludeRecurrences = False
        oRestrictedItems.Sort("[Start],[Subject]")

        Dim BodyMarker As String = " [TENA10]"

        pbProgress.Maximum = oRestrictedItems.Count

        For Each oAppointment As Microsoft.Office.Interop.Outlook.AppointmentItem In oRestrictedItems
            pbProgress.Value += 1
            Try
                If oAppointment.Body.Contains(BodyMarker) Then
                    lblStatus.Text = "Processing: " + oAppointment.Subject
                    oAppointment.Categories = Nothing
                    If chkDisableReminders.Checked = True Then
                        Try
                            oAppointment.ReminderSet = False
                            oAppointment.Save()
                        Catch ex As Exception
                            MsgBox("Unable to clear appointment reminder - " + ex.Message, MsgBoxStyle.Exclamation, "Error clearing appoitnment reminder")
                        End Try
                    End If

#If Debug = True Then
                    Debug.WriteLine(oAppointment.Subject + " = " + oAppointment.Body)
#End If
                    Dim oOneNoteApplication As Microsoft.Office.Interop.OneNote.ApplicationClass = New Microsoft.Office.Interop.OneNote.ApplicationClass()
                    Try

                        oOneNoteApplication.GetSpecialLocation(OneNote.SpecialLocation.slUnfiledNotesSection, strUnfiledNotesPath)
                        Dim UnfiledNotesID As String = ""
                        oOneNoteApplication.OpenHierarchy(strUnfiledNotesPath, "", UnfiledNotesID, OneNote.CreateFileType.cftNone)
                        oOneNoteApplication.CreateNewPage(UnfiledNotesID, strNewPageID, Microsoft.Office.Interop.OneNote.NewPageStyle.npsBlankPageWithTitle)

                        Dim NewPageXML As String = FormattedOneNotePage.ToString
                        Dim Location As String
                        If oAppointment.Location = Nothing Then
                            Location = "Unknown"
                            NewPageXML = NewPageXML.Replace("{PageColor}", "automatic")
                        Else
                            Location = oAppointment.Location.Replace("Rm ", "Room ")
                        End If

                        Select Case oAppointment.Subject.Substring(0, 3)
                            Case "BIC", "BIU", "BIO", "BIE", "BIP"
                                NewPageXML = NewPageXML.Replace("{PageColor}", "#F5E2FF").Replace("{TitleFontColor}", ";color:#6600CC")
                            Case "WPH", "WEM", "UNC"
                                NewPageXML = NewPageXML.Replace("{PageColor}", "#FFFADF").Replace("{TitleFontColor}", ";color:#FF6600")
                            Case "DAT", "OSP", "COS"
                                NewPageXML = NewPageXML.Replace("{PageColor}", "#ECF0E5").Replace("{TitleFontColor}", ";color:green")
                            Case "ASI", "ARC", "WCL", "WEB", "WEM", "WSV"
                                NewPageXML = NewPageXML.Replace("{PageColor}", "#EDF5FE").Replace("{TitleFontColor}", ";color:blue")
                            Case "DEV", "DPR"
                                NewPageXML = NewPageXML.Replace("{PageColor}", "#FFEEEF").Replace("{TitleFontColor}", ";color:red")

                            Case "BOF", "PRC"
                                NewPageXML = NewPageXML.Replace("{PageColor}", "#FCE5FB").Replace("{TitleFontColor}", ";color:red")
                            Case "MGT", "SIA", "VIR"
                                NewPageXML = NewPageXML.Replace("{PageColor}", "#DDDEBD").Replace("{TitleFontColor}", ";color:#61380B")

                            Case Else
                                NewPageXML = NewPageXML.Replace("{PageColor}", "automatic").Replace("{TitleFontColor}", "")
                        End Select

                        NewPageXML = NewPageXML.Replace("{PageID}", strNewPageID).Replace("{Title}", oAppointment.Subject.Replace("&", "&amp;")).Replace("{Body}", oAppointment.Body.Replace("<", "&lt;").Replace(">", "&gt;").Replace("&", "&amp;")).Replace("{RoomNumber}", Location).Replace("{DateAndTime}", String.Format("{0:yyyy-MM-ddTHH:mm:ss.000Z}", oAppointment.StartUTC))

                        Select Case oAppointment.Subject.Substring(0, 3)
                            Case "ASI"
                                SetTrack(NewPageXML, oAppointment, "Application Server & Infrastructure")
                            Case "ARC"
                                SetTrack(NewPageXML, oAppointment, "Architecture")
                            Case "BIC"
                                SetTrack(NewPageXML, oAppointment, "BI: Customer Business Value")
                            Case "BIU"
                                SetTrack(NewPageXML, oAppointment, "BI: Empower End Users")
                            Case "BIO"
                                SetTrack(NewPageXML, oAppointment, "BI: Improve Organizational Effectiveness")
                            Case "BIE"
                                SetTrack(NewPageXML, oAppointment, "BI: Increase IT and Developer Efficiency")
                            Case "BIP"
                                SetTrack(NewPageXML, oAppointment, "BI: Partner Services & Solutions Delivery")
                            Case "COS"
                                SetTrack(NewPageXML, oAppointment, "Cloud Computing & Online Services")
                            Case "DAT"
                                SetTrack(NewPageXML, oAppointment, "Database Platform")
                            Case "DEV"
                                SetTrack(NewPageXML, oAppointment, "Developer Tools: Languages & Frameworks")
                            Case "DPR"
                                SetTrack(NewPageXML, oAppointment, "Development Practices")
                            Case "MGT"
                                SetTrack(NewPageXML, oAppointment, "Management")
                            Case "OSP"
                                SetTrack(NewPageXML, oAppointment, "Office & SharePoint")
                            Case "SIA"
                                SetTrack(NewPageXML, oAppointment, "Security, Identity & Access")
                            Case "UNC"
                                SetTrack(NewPageXML, oAppointment, "Unified Communications")
                            Case "VIR"
                                SetTrack(NewPageXML, oAppointment, "Virtualization")
                            Case "WEB"
                                SetTrack(NewPageXML, oAppointment, "Web Platform")
                            Case "WCL"
                                SetTrack(NewPageXML, oAppointment, "Windows Client")
                            Case "WEM"
                                SetTrack(NewPageXML, oAppointment, "Windows Embedded")
                            Case "WPH"
                                SetTrack(NewPageXML, oAppointment, "Windows Phone")
                            Case "WSV"
                                SetTrack(NewPageXML, oAppointment, "Windows Server")
                            Case "BOF"
                                If oAppointment.Subject.Contains("-IT - ") Then
                                    SetTrack(NewPageXML, oAppointment, "Birds-of-a-Feather - IT Pro")
                                ElseIf oAppointment.Subject.Contains("-DV - ") Then
                                    SetTrack(NewPageXML, oAppointment, "Birds-of-a-Feather - Developer")
                                Else
                                    SetTrack(NewPageXML, oAppointment, "Birds-of-a-Feather")
                                End If
                            Case Else
                                SetTrack(NewPageXML, oAppointment, "Unknown")

                                'Case "CRM"
                                '    NewPageXML = NewPageXML.Replace("{Track}", "Cram4Exam")
                                'Case "DYN"
                                '    NewPageXML = NewPageXML.Replace("{Track}", "Dynamics")
                                'Case "INF"
                                '    NewPageXML = NewPageXML.Replace("{Track}", "Infrastructure for Developers")
                                'Case "SOA"
                                '    NewPageXML = NewPageXML.Replace("{Track}", "SOA and Business Processes")
                                'Case "WIT"
                                '    NewPageXML = NewPageXML.Replace("{Track}", "Women in Technology")
                                'Case "WUX"
                                '    NewPageXML = NewPageXML.Replace("{Track}", "User Experience")
                                'Case "TEO"
                                '    NewPageXML = NewPageXML.Replace("{Track}", "TechEd Online")
                                'Case "LUN"
                                '    NewPageXML = NewPageXML.Replace("{Track}", "Lunch Sessions")

                        End Select

                        If oAppointment.Subject.Contains("TLC") Then
                            SetType(NewPageXML, oAppointment, "TLC Demo Station")
                        ElseIf oAppointment.Subject.Contains("BRK") Then
                            SetType(NewPageXML, oAppointment, "Breakout Session")
                        ElseIf oAppointment.Subject.Contains("INT") Then
                            SetType(NewPageXML, oAppointment, "Interactive Session")
                        ElseIf oAppointment.Subject.Contains("HOL") Then
                            SetType(NewPageXML, oAppointment, "Hands-On Lab")
                        ElseIf oAppointment.Subject.Contains("BOF") Then
                            SetType(NewPageXML, oAppointment, "Birds of a Feather")
                        ElseIf oAppointment.Subject.Contains("CER") Then
                            SetType(NewPageXML, oAppointment, "Certification Preparation")
                        ElseIf oAppointment.Subject.Contains("WIT") Then
                            SetType(NewPageXML, oAppointment, "Women in Technology")
                        ElseIf oAppointment.Subject.Contains("KEY") Then
                            SetType(NewPageXML, oAppointment, "Keynote")
                        ElseIf oAppointment.Subject.Contains("PRC") Then
                            SetType(NewPageXML, oAppointment, "Pre-Conference Seminar")
                        ElseIf oAppointment.Subject.Contains("TE101") Then
                            SetType(NewPageXML, oAppointment, "Tech-Ed 101")
                        ElseIf oAppointment.Subject.Contains("ILL") Then
                            SetType(NewPageXML, oAppointment, "Instructor-Led Lab")
                        Else
                            SetType(NewPageXML, oAppointment, "Breakout Session")
                        End If

#If Debug = True Then
                        Debug.WriteLine(NewPageXML)
#End If

                        oOneNoteApplication.UpdatePageContent(NewPageXML)
                        oAppointment.Save()
                    Catch ex As Exception
                        MsgBox("Error creating OneNote Page for " + oAppointment.Subject + " - " + ex.Message, MsgBoxStyle.Exclamation, "Error creating OneNote Page")
                    End Try
                Else
                    lblStatus.Text = "Skipping Non-TechEd Appointment"
                End If
            Catch ex As Exception

            End Try
        Next

        lblStatus.Text = "Done.  Enjoy the conference!"

        oNamespace.Logoff()
        oOutlookApplication = Nothing
        oNamespace = Nothing


    End Sub


    Private Sub SetTrack(ByRef OneNotePageXML As String, ByRef ThisAppointment As Microsoft.Office.Interop.Outlook.AppointmentItem, ByVal Track As String)
        OneNotePageXML = OneNotePageXML.Replace("{Track}", Track)
        If chkSetCategoryForTrackInOutlook.Checked Then
            ThisAppointment.Categories = Track.Replace(",", " ")
        End If
    End Sub

    Private Sub SetType(ByRef OneNotePageXML As String, ByRef ThisAppointment As Microsoft.Office.Interop.Outlook.AppointmentItem, ByVal SessionType As String)
        OneNotePageXML = OneNotePageXML.Replace("{SessionFormat}", SessionType)
        If chkSetCategoryForSessionTypeInOutlook.Checked Then
            ThisAppointment.Categories = String.Format("{0};{1}", ThisAppointment.Categories, SessionType)
        End If
    End Sub

End Class
